package com.longrise.android.byjk.plugins.aboutme.personalInfo.person_honor;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.ui.dialog.ActionSheetDialog;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.PrintLog;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/3/12.
 * 个人荣誉删除页面
 */

public class PersonHonorEditActivity extends BaseActivity2<PersonHonorShowPresenter> implements PersonHonorShowContract.View,
                                                PersonHonorEditAdapter.OnItemClickListener,View.OnClickListener{

    public static final String TAG = "PersonHonorEditActivity";
    private RecyclerView mRcv;
    private PersonHonorEditAdapter mAdapter;
    private RelativeLayout mRlhandle;
    private TextView mTvSelect;
    private TextView mTvDelete;
    private List<PersonHonorBean> mSelectedHonorBeans = new ArrayList<>();
    private List<PersonHonorBean> mHonorBeanList ;
    private boolean isChecked;
    private List<String> honorIds;
    private RelativeLayout mRlHonorList;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_person_honor_save;
    }

    @Override
    public void initView() {
        mRlHonorList = (RelativeLayout) findViewById(R.id.rl_person_honor_edit_rcv);
        mRcv = (RecyclerView) findViewById(R.id.rcv_person_honor);
        mRlhandle = (RelativeLayout) findViewById(R.id.rl_honor_handle);
        mTvSelect = (TextView) findViewById(R.id.tv_honor_select);
        mTvDelete = (TextView) findViewById(R.id.tv_honor_delete);
        mToolbar.setNavigationIcon(null);
        mToolbar.setTitleMarginStart(AppUtil.dip2px(16));
        setToolbarTitle(AppUtil.getString(R.string.person_honor));
        setToolbarRightIvTitle(R.drawable.umeng_by_ic_close_black);
        initRcv();
        initEvent();
    }

    @Override
    public void onToolbarBackClick() {
        finish();
    }

    private void initRcv() {
        View footerView = View.inflate(mContext, R.layout.item_person_honor_footer, null);
        mAdapter = new PersonHonorEditAdapter();
        mAdapter.addFooterView(footerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRcv.setLayoutManager(layoutManager);
        mRcv.setAdapter(mAdapter);
        mPresenter.getPersonHonorData();
    }

    public void initEvent(){
        mTvSelect.setOnClickListener(this);
        mTvDelete.setOnClickListener(this);
        mAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

            //点击全选
            case R.id.tv_honor_select:
                isChecked = !isChecked;
                if (isChecked) {
                    if(mHonorBeanList !=null){
                        mSelectedHonorBeans.clear();
                        mSelectedHonorBeans.addAll(mHonorBeanList);
                        mTvSelect.setText(AppUtil.getString(R.string.select_cancel));
                    }
                }else {
                    mSelectedHonorBeans.clear();
                    mTvSelect.setText(AppUtil.getString(R.string.select_all));
                }

                if(mSelectedHonorBeans.size() == 0){
                    mTvDelete.setTextColor(Color.parseColor("#999999"));
                    mTvDelete.setClickable(false);
                    mTvDelete.setEnabled(false);
                }else {
                    mTvDelete.setTextColor(Color.parseColor("#FC0000"));
                    mTvDelete.setClickable(true);
                    mTvDelete.setEnabled(true);
                }
                mAdapter.setSelectedCourseBeans(mSelectedHonorBeans);
                break;

            //点击删除
            case R.id.tv_honor_delete:
                deleteHonorDialog();
                break;
        }
    }

    public void deleteHonorData(){
        honorIds = new ArrayList<>();
        for (int i = 0; i < mSelectedHonorBeans.size(); i++) {
            String id = mSelectedHonorBeans.get(i).getHonorId();
            honorIds.add(id);
        }

        mPresenter.deletePersonHonor(honorIds);
    }


    @Override
    public void refreshData(List<PersonHonorBean> mHonorBeanList) {
        this.mHonorBeanList = mHonorBeanList;
        mSelectedHonorBeans.clear();
        PrintLog.e(TAG,"mHonorBeanList=:"+mHonorBeanList.toString());
        if(mHonorBeanList.size() != 0){
            mAdapter.setNewData(mHonorBeanList);
            mRlHonorList.setVisibility(View.VISIBLE);
            mRlhandle.setVisibility(View.VISIBLE);
        }else {
            mRlhandle.setVisibility(View.GONE);
            mRlHonorList.setVisibility(View.GONE);
            mAdapter.setNewData(null);
        }
    }

    @Override
    public void responseDelteData(EntityBean bean) {
        mPresenter.getPersonHonorData();
        EntityBean honorBean = bean.getBean("result");
        int honorlength = honorBean.getInt("honorlength");
        if(honorlength == 0){
            finish();
            PersonHonorEvent event = new PersonHonorEvent();
            event.setRefresh(true);
            EventBus.getDefault().post(event);
        }
    }


    @Override
    public void onItemClick(PersonHonorBean honorBean) {
        //表示勾选checkbox

            if (mSelectedHonorBeans.contains(honorBean)) {
                mSelectedHonorBeans.remove(honorBean);
            } else {
                mSelectedHonorBeans.add(honorBean);
            }

            if(mSelectedHonorBeans.size() == mHonorBeanList.size()){
                mTvSelect.setText(AppUtil.getString(R.string.select_cancel));
                isChecked = true;
            }else {
                mTvSelect.setText(AppUtil.getString(R.string.select_all));
                isChecked = false;
            }

            if(mSelectedHonorBeans.size() == 0){
                mTvDelete.setTextColor(Color.parseColor("#999999"));
                mTvDelete.setClickable(false);
                mTvDelete.setEnabled(false);
            }else {
                mTvDelete.setTextColor(Color.parseColor("#FC0000"));
                mTvDelete.setClickable(true);
                mTvDelete.setEnabled(true);
            }

            mAdapter.setSelectedCourseBeans(mSelectedHonorBeans);
    }

    private void deleteHonorDialog() {
        new ActionSheetDialog(this).builder().setCancelable(false).setCanceledOnTouchOutside(false).addSheetItem(getString(R.string.product_delete_title), ActionSheetDialog.SheetItemColor.Black, new ActionSheetDialog.OnSheetItemClickListener() {
            @Override
            public void onClick(int which) {
            }
        }).addSheetItem(getString(R.string.clear), ActionSheetDialog.SheetItemColor.Red, new ActionSheetDialog.OnSheetItemClickListener() {
            @Override
            public void onClick(int which) {
                deleteHonorData();
            }
        }).setCancelColor(ActionSheetDialog.SheetItemColor.BlueDeep).setOnCancelListener(new ActionSheetDialog.OncancelListener() {
            @Override
            public void onCancel() {

            }
        }).show();
    }

    @Override
    public void onToolbarRightIvClick() {
        super.onToolbarRightIvClick();
        finish();
        PersonHonorEvent event = new PersonHonorEvent();
        event.setRefresh(true);
        EventBus.getDefault().post(event);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            finish();
            PersonHonorEvent event1 = new PersonHonorEvent();
            event1.setRefresh(true);
            EventBus.getDefault().post(event1);
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}
